import axios from "axios";
import { useJinnongStore } from "../stores/jinnong";
import { ElMessage, ElMessageBox } from "element-plus";

const BASE_URL = import.meta.env.VITE_API_URL;

const request = axios.create({
  baseURL: BASE_URL,
  timeout: 20000,
});

// request拦截器
request.interceptors.request.use(
  (config) => {
    const store = useJinnongStore();
    config.headers["authorization"] = "Bearer " + store.token;
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);

// response 拦截器
request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    console.log("request error", error);
    if (error.response.status === 424) {
      ElMessageBox.alert("登录已过期，请重新登录", "提示", {
        confirmButtonText: "OK",
      });
    }
    return Promise.reject(error);
  }
);

export function getCameras(workId) {
  return request({
    url: "/oaworkmonitoring/list/" + workId,
    method: "get",
  });
}

export function getPlaybackUrl(data) {
  return request({
    url: "/hik/artemis?path=/api/video/v2/cameras/playbackURLs",
    method: "post",
    data,
  });
}
